package com.urbo.android.shortcutter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

/**
 * AlertDialog wrapper.  This class is used to show dialog where you can set icon and title of the new shortcut.
 * 
 * @author Radovan Urban
 *
 */
public final class ShortcutDialog extends UrboDialog {
	
	private ListSelectionDialog iconListDialog;	// Dialog do display after click on icon image
	private List<Map<String, String>> listOfIconSources; // Data to diplay in iconListDialog
	private GridDialog iconGridDialog;			// 
	
	private boolean reloadNeeded;				// onCreate and after download is likely to reload list of icon sources
	public static String ITEM_TITLE = "title";
	public static String ITEM_IDENTIFIER = "identifier";
	public static String ITEM_TYPE = "type";
	
	public static String TYPE_BUILDIN = "0";
	public static String TYPE_APPLICATION = "1";
	public static String TYPE_ANDROID = "2";
	public static String TYPE_IPACK = "3";
	public static String TYPE_DOWNLOAD = "4";
	public static String TYPE_CUSTOM = "4";

	/**
	 * Create one item if list of icon sources. Each item is map of 3 keys. Identifier, Title and Type
	 * @param label name of the package
	 * @param className class name of the package(activity)
	 * @param 
	 * @return Item witch several mapped records
	 */
	private Map<String, String> createItem(String title, String identifier, String type){//, String image) {  

	    Map<String,String> item = new HashMap<String,String>();  
	    item.put(ITEM_TITLE, title);  
	    item.put(ITEM_IDENTIFIER, identifier);
	    item.put(ITEM_TYPE, type);
	    return item;  
	}
	
	/**
	 * Create list of mapped list with possible icon sources.
	 * Application source 	- icons from packages installed in system
	 * Download				- Link to Android market to download Ipack packages 
	 * Ipack sources		- Installed Ipack packages installed in system
	 */
	private void createListOfIconSources() {
		 
		// listOfIconSources.add(createItem("Application icon", "app", TYPE_BUILDIN));
		if (listOfIconSources == null) {
			listOfIconSources = new LinkedList<Map<String,String>>();
		} else {
			listOfIconSources.clear();
		}
		
		listOfIconSources.add(createItem("Application icon", "app", TYPE_APPLICATION));
		
		// query icon pack in iPack format, installed in system
		mContext.sendOrderedBroadcast(
	    	new Intent( IpackKeys.Actions.QUERY_PACKS ),
	    	null,
	    	new BroadcastReceiver() {
	    		public void onReceive( Context c, Intent i ) {

	    			Bundle packs = getResultExtras( true );
	    				
	    			for ( String pname : packs.keySet() ) {
	    				listOfIconSources.add(createItem((( Bundle )packs.get( pname )).getString( IpackKeys.Extras.LABEL ), pname, TYPE_IPACK));
	       			}
	    			listOfIconSources.add(createItem("Download more icons", "app", TYPE_DOWNLOAD));
	    			iconListDialog.showReload(listOfIconSources);
	    		}
	    	}, null, Activity.RESULT_OK, null, null
	    );
	}
	
	public ShortcutDialog(Context context, int layoutRecourceId, DialogInterface.OnClickListener dialogClickListener) {
		
		super(context, layoutRecourceId, dialogClickListener);
		((ImageView)mUrboDialogLayout.findViewById(R.id.shortcut_icon)).setOnClickListener(iconClickListener);
		iconListDialog = new ListSelectionDialog (context);
		iconListDialog.SetOnClickListener(onIconSourceClickListener);
		reloadNeeded = true; 
	}

	
	// Click Handler of itemClick in ListSelectionDialog. Source of icons is selected 
	public ListSelectionDialog.OnClickListener onIconSourceClickListener = new ListSelectionDialog.OnClickListener() {
		
		@Override
		public void onClick(Map<String, String> selectedItem) {
			
			iconListDialog.dismiss();
			
			if  (iconGridDialog == null) {
				iconGridDialog = new GridDialog (mContext);
				iconGridDialog.SetOnClickListener(onIconGridClickListener);
			}
			
			String typeOfIconSource = selectedItem.get(ITEM_TYPE);
			
			if (typeOfIconSource == TYPE_APPLICATION) {
				iconGridDialog.setAplicationIconSource();
			} else if (typeOfIconSource == TYPE_IPACK) {
				iconGridDialog.setIpkgIconSource(selectedItem.get(ITEM_IDENTIFIER));
			} else if (typeOfIconSource == TYPE_DOWNLOAD) {
				Uri marketUri = Uri.parse(IpackKeys.ICON_DOWNLOAD_URI_MARKET);
				Intent marketIntent = new Intent(Intent.ACTION_VIEW, marketUri);
				mContext.startActivity(marketIntent);
				reloadNeeded = true;
				return;
			}
		}
	};

	// Click handler of itemClick in iconGridDialog. Icon is selected
	public GridDialog.OnClickListener onIconGridClickListener = new GridDialog.OnClickListener() {
		@Override
		public void onClick(Drawable d) {
			iconGridDialog.dismiss();
			setShortcutIcon(d);
		}
	};
	

	/**
	 * When user click on image in this dialog, list of possible icon sources is shown
	 */
	public ImageView.OnClickListener iconClickListener = new ImageView.OnClickListener(){
		@Override
		public void onClick(View v) {
			if (reloadNeeded) {
				reloadNeeded = false;
				createListOfIconSources();
			} else {
				iconListDialog.show();
			}
		}
	};
	
	public String getShortcutTitle() {
		return ((EditText) mUrboDialogLayout.findViewById(R.id.shortcut_title)).getText().toString();	
	}
	
	public void setShortcutTitle(String title) {
		EditText dialogEditText = (EditText) mUrboDialogLayout.findViewById(R.id.shortcut_title);		
		dialogEditText.setText(title);
	}
	
	public void setShortcutIcon(Drawable icon) {
		((ImageView)mUrboDialogLayout.findViewById(R.id.shortcut_icon)).setImageDrawable(icon);
	}
	
	public Drawable getShortcutIcon() {
		return ((ImageView)mUrboDialogLayout.findViewById(R.id.shortcut_icon)).getDrawable();
	}
	

	
}
